//
//  ErWeiMaViewController.m
//  KongFuCenter
//
//  Created by wolaji on 15/8/5.
//  Copyright (c) 2015年 zykj.LikeAttention. All rights reserved.
//

#import "ErWeiMaViewController.h"

#import "ConfirmOrdersViewController.h"
#import "SVProgressHUD.h"
#import "CardDetialViewController.h"
@interface ErWeiMaViewController ()

@property (nonatomic, assign) BOOL is_OK;

@end

@implementation ErWeiMaViewController
{
    CardDetialViewController * cardDetialViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    cardDetialViewController = [[CardDetialViewController alloc] init];
    [self p_navigationController];
    

    
}

#pragma mark - navigationController
- (void)p_navigationController
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.navigationItem.title = @"扫描二维码";
    UIButton * btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [btn setBackgroundImage:[UIImage imageNamed:@"返回"] forState:(UIControlStateNormal)];
        btn.frame = CGRectMake(10, 20, 25, 30);
    [btn addTarget:self action:@selector(leftBarButtonItemAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    
    _captureSession = nil;
    _isReading = NO;
    _lblStatus.hidden = NO;
    
    
    [self startReading];
}
//点击事件(左)
- (void)leftBarButtonItemAction:(UIBarButtonItem *)bar
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 二维码
- (BOOL)startReading {
    
    _viewPreview.frame=CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64);
    
    NSError *error;
    
    //1.初始化捕捉设备（AVCaptureDevice），类型为AVMediaTypeVideo
    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    //2.用captureDevice创建输入流
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
    if (!input) {
        NSLog(@"%@", [error localizedDescription]);
        return NO;
    }
    
    //3.创建媒体数据输出流
    AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
    
    //4.实例化捕捉会话
    _captureSession = [[AVCaptureSession alloc] init];
    
    //4.1.将输入流添加到会话
    [_captureSession addInput:input];
    
    //4.2.将媒体输出流添加到会话中
    [_captureSession addOutput:captureMetadataOutput];
    
    //5.创建串行队列，并加媒体输出流添加到队列当中
    dispatch_queue_t dispatchQueue;
    dispatchQueue = dispatch_queue_create("myQueue", NULL);
    //5.1.设置代理
    [captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
    
    //5.2.设置输出媒体数据类型为QRCode
    [captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];
    
    //6.实例化预览图层
    _videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
    
    //7.设置预览图层填充方式
    [_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    
    //8.设置图层的frame
    [_videoPreviewLayer setFrame:_viewPreview.layer.bounds];
    
    //9.将图层添加到预览view的图层上
    [_viewPreview.layer addSublayer:_videoPreviewLayer];
    
    //10.设置扫描范围
    captureMetadataOutput.rectOfInterest = CGRectMake(0.2f, 0.2f, 0.8f, 0.8f);
    
    //10.1.扫描框
    _boxView = [[UIView alloc] initWithFrame:CGRectMake(_viewPreview.bounds.size.width * 0.2f, _viewPreview.bounds.size.height * 0.2f, _viewPreview.bounds.size.width - _viewPreview.bounds.size.width * 0.4f, _viewPreview.bounds.size.width - _viewPreview.bounds.size.width * 0.4f)];
    _boxView.layer.borderColor = [UIColor greenColor].CGColor;
    _boxView.layer.borderWidth = 1.0f;
    
    [_viewPreview addSubview:_boxView];
    
    //10.2.扫描线
    _scanLayer = [[CALayer alloc] init];
    _scanLayer.frame = CGRectMake(0, 0, _boxView.bounds.size.width, 1);
    _scanLayer.backgroundColor = [UIColor brownColor].CGColor;
    
    [_boxView.layer addSublayer:_scanLayer];
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2f target:self selector:@selector(moveScanLayer:) userInfo:nil repeats:YES];
    
    [timer fire];
    
    //10.开始扫描
    [_captureSession startRunning];
    
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)stopReading{
    
    [_captureSession stopRunning];
    _captureSession = nil;
    [_scanLayer removeFromSuperlayer];
//    [_videoPreviewLayer removeFromSuperlayer];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(HUDDismiss) name:@"HUDDismiss" object:nil];
    
    [SVProgressHUD showWithStatus:@"正在验证二维码，请稍后"];
    
//    if(self.is_OK == YES)
//    {
//        _lblStatus.text = @"请等待,加载数据比较慢";
//    }
}

- (void)HUDDismiss
{
    [SVProgressHUD dismiss];
}

#pragma mark - AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    [self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
    //判断是否有数据
    if (metadataObjects != nil && [metadataObjects count] > 0) {
        AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
        
//        [SVProgressHUD showWithStatus:@"加载中，请稍后。。。"];
        
        //判断回传的数据类型
        if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
            
            if([[metadataObj stringValue] containsString:@"http"])
            {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[metadataObj stringValue]]];
                
                self.is_OK = NO;
            }
            else
            {
                NSLog(@"%@",[metadataObj stringValue]);
                
                cardDetialViewController.couponid = [metadataObj stringValue];
                
                [self showViewController:cardDetialViewController sender:nil];
                
//                [self.navigationController pushViewController:cardDetialViewController animated:YES];
                
                self.is_OK = YES;
            }

            
            
//            if([[[metadataObj stringValue] substringToIndex:4] isEqualToString:@"http"])
//            {
////                [SVProgressHUD dismiss];
//
//            }
//            else
//            {
////                NSLog(@"跳到卡券提示页");
////                self.label_tip.hidden = NO;
//                
//
//            }
            
            
            
            _isReading = NO;
        }

    }
}

- (void)moveScanLayer:(NSTimer *)timer
{
    CGRect frame = _scanLayer.frame;
    if (_boxView.frame.size.height < _scanLayer.frame.origin.y) {
        frame.origin.y = 0;
        _scanLayer.frame = frame;
    }else{
        
        frame.origin.y += 5;
        
        [UIView animateWithDuration:0.1 animations:^{
            _scanLayer.frame = frame;
        }];
    }
}

- (BOOL)shouldAutorotate
{
    return NO;
}
@end
